package xc.com.ui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

public class RemoteController {
private Context h;
final String TAG="RemoteController";
	public RemoteController(Context context){
		this.h=context;
	}
	
	
	public boolean a(String IP, String paramString)
	  {
	    boolean k = false;
	    if ((paramString == null) || ("".equals(paramString))){
	    	
	    }
	    else
	    {
	      //label15: return k;
	      String str1 = "adb -s " + IP + ":5555  install -r " + paramString;
	     // com.qihoo.tvframework.qlog.a.b("connect", str1);
	      try
	      {
	        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(str1).getInputStream()));
	        StringBuffer localStringBuffer = new StringBuffer();
	        String str2 = localBufferedReader.readLine();
	        Log.e("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", str2);
	        if (str2 != null)
	        {
	        	localStringBuffer.append(str2);
	         // com.qihoo.tvframework.qlog.a.b("connect", str1 + "____rlt is " + localStringBuffer.toString());
	          if (localStringBuffer.toString().contains("Success")){
		          k = true;

	          }
	          if (localStringBuffer.toString().contains("Failure")){
		          k = false;

	          }
	        }
	        
	      }
	      catch (IOException localIOException)
	      {
	        localIOException.printStackTrace();
	      }
	    }
		return k;
	  }

	
	
	
	
	
	
	
	
	
	public String a(InputStream inputStream,File file){
		int bytesWritten = 0;
		int byteCount = 0;
		try {
			int cont=inputStream.available();
			byte[] bytes = new byte[cont];


			OutputStream out=new FileOutputStream(file.toString());
			
			while ((byteCount = inputStream.read(bytes)) != -1)
			{
			          out.write(bytes, bytesWritten, byteCount);
			           bytesWritten += byteCount;
			}
			inputStream.close();
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 Log.e(TAG, file.toString()) ;
		return file.toString();
	}
	
	 public String f()
	  {
		 Log.e(TAG, "wwwwwwwwwwwwwwwwwwwwwwwwwwww") ;
	    String  localObject = null;
	    //com.qihoo.tvframework.qlog.a.b("connect", "write asset apk");
	    AssetManager localAssetManager = this.h.getAssets();
	    try
	    {
	      InputStream localInputStream = localAssetManager.open("tvstore.mp3");
	      if (localInputStream != null)
	      {
	        String str1 = Environment.getExternalStorageDirectory().getPath();
	        File localFile1 = new File(str1);
	        if (!localFile1.exists()){
	        	localFile1.mkdir();
	        }
	         Log.e(TAG, "XXXXXXXXXXXXXX") ;
	        File localFile2 = new File(str1 + "/test.apk");

	        if (localFile2.exists()){
	        	 //localFile2.delete();
	 	        localObject = localFile2.toString();

	        }else{
	        	  localFile2.createNewFile();
	  	        String str2 = a(localInputStream, localFile2);
	  	        Log.e(TAG, str2) ;
	   	        localObject = str2;
	        }

 	      
	      }
	    }
	    catch (IOException localIOException)
	    {
	      localIOException.printStackTrace();
	    }
	    return localObject;
	    
	  }
}
